// hmc
import Vue from 'vue'
//递归
export function getAllRow(childs, array,costAllocationFlag) {
    if (!childs) {
        return;
    }

    childs.forEach((f, fi) => {

        Vue.set(f, 'isCheck', false);
        Vue.set(f, 'isIndeterminateCheck', false);
        if(costAllocationFlag && costAllocationFlag == '1' && !f.parentAllocationScale && !f.merchantAllocationScale){
            f.parentAllocationScale = 0;
            f.merchantAllocationScale = 100;
        }
        if(costAllocationFlag && costAllocationFlag == '1' && !f.deptShare && !f.merchantShare){
            f.deptShare = 0;
            f.merchantShare = 100;
        }
        getAllRow(f.children, array,costAllocationFlag);

        array.push(f);
    });

    return array;
}
//找子
export function changeChild(row, checked) {

    //  row.isCheck = checked;



    if (row.children) {
        row.children.forEach(element => {

            element.isCheck = checked;

            changeChild(element, checked);
        });
    }
}
//找父
export function changeParent(row, allData) {
    if (!row.mpParentId) {
        return;
    }

    let parentRow = allData.filter(f => {
        return f.mpId == row.mpParentId;
    })[0];

    if (!parentRow) {
        return;
    }

    let childs = allData.filter(f => {
        return f.mpParentId == parentRow.mpId;
    });

    let selection = allData.filter(f => { return f.isCheck; });


    let selectionIds = selection.map(m => {
        return m.mpId;
    });

    let allCheck = childs.every(e => {
        return selectionIds.includes(e.mpId);
    });

    let someCheck = childs.some(e => {
        return selectionIds.includes(e.mpId);
    });

    //  

    parentRow.isCheck = someCheck;
    parentRow.isIndeterminateCheck = someCheck && !allCheck;
    changeParent(parentRow, allData);
}

/**
 * 重新渲染组件 
 */
export function reloadAllCheckedStatus(allData){
    allData.forEach(item=>{
        item.isCheck = false;
    })
}


